// pages/yijianFankui/yijianFankui.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    textLength: 0,
    xuanTiyle: [{
      'flags': true
    }, {
      'flags': false
    }, {
      'flags': false
    }, {
      'flags': false
    }],
    titles: "功能异常：功能故障或者不可用",
    xiangxitext: '',
    phones: '',
    tupianList: [],
  },
  //选中标题
  xuanZhong(e) {
    let that = this
    // console.log(e)
    let titles = e.currentTarget.dataset.text
    let xuanTiyle = that.data.xuanTiyle
    let indexs = e.currentTarget.dataset.index
    for (let i = 0; i < xuanTiyle.length; i++) {
      xuanTiyle[i].flags = false
    }
    xuanTiyle[indexs].flags = true
    that.setData({
      xuanTiyle: xuanTiyle,
      titles: titles
    })

  },
  //获取输入值
  bindTextAreaBlur: function(e) {
    console.log(e)
    this.setData({
      textLength: e.detail.cursor,
      xiangxitext: e.detail.value
    })
  },
  //获取输入的电话
  getphone(e) {
    this.setData({
      phones: e.detail.value
    })
  },
  addImg() {
    let that = this
    let oldtupianList = that.data.tupianList
    let num = 4 - oldtupianList.length
    wx.chooseImage({
      count: num,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        setTimeout(function() {
          wx.showLoading({
            title: '上传中',
            mask: true
          })
        }, 0)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        let list = []
        for (let i in tempFilePaths) {
          list.push(wx.getFileSystemManager().readFileSync(tempFilePaths[i], "base64"))
        }
        console.log(list)
        wx.request({
          url: app.globalData.myurl,
          method: 'POST',
          data: {
            cmd: "uploadPluralImg",
            imgList: JSON.stringify(list),
          },
          header: {
            "content-type": "application/x-www-form-urlencoded",
            "token": wx.getStorageSync('token')
          },
          success(res) {
            if (res.data.zjstate == 1) {
              setTimeout(function () {
                wx.hideLoading()
                wx.stopPullDownRefresh() //停止下拉刷新
              }, 0)
              setTimeout(function () {
                wx.showModal({
                  title: '您的账号已在其他设备登录，请重新登录！',
                  showCancel: false, //是否显示取消按钮false去掉取消按钮
                  success: function (res) {
                    if (res.confirm) {
                      wx.clearStorageSync()
                      wx.reLaunch({
                        url: '/pages/login/login',
                      })
                    } else {
                      wx.clearStorageSync()
                      wx.reLaunch({
                        url: '/pages/login/login',
                      })
                    }
                  }
                })
              }, 0)
              return
            }
            console.log(res)
            if (res.data.result == 0) {
              for (let j in res.data.imgUrllist) {
                oldtupianList.push(res.data.imgUrllist[j])
              }
              that.setData({
                tupianList: oldtupianList
              })
            }
            setTimeout(function() {
              wx.hideLoading()
            }, 0)
            setTimeout(function() {
              wx.showToast({
                title: res.data.resultNote,
                icon: 'none',
                duration: 1500
              })
            }, 0)
          }
        });
      }

    })

  },
  shanchu(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.tupianList
    tupianList.splice(indexs, 1)
    this.setData({
      tupianList: tupianList
    })
  },
  tijiaofk() {
    let that = this
    if (!that.data.phones) {
      wx.showToast({
        title: '请填写手机号码，方便我们与您联系!',
        icon: 'none'
      })
      return
    }
    var telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    var inputStr = that.data.phones
    if (!(telStr.test(inputStr))) {
      wx.showToast({
        title: '手机号码输入不规范',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (!that.data.xiangxitext) {
      wx.showToast({
        title: '请补充问题描述，便于我们迅速处理',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "feedbackInfo",
        userId: wx.getStorageSync('userId'),
        quest: that.data.titles,
        phone: that.data.phones,
        detail: that.data.xiangxitext,
        imgs: JSON.stringify(that.data.tupianList)
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        setTimeout(function() {
          wx.hideLoading()
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        if (res.data.result == 0) {
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})